package com.denso.store.admin.scheduler;

import com.denso.store.common.AdminOpt;
import com.denso.store.common.Status;
import com.denso.store.common.model.Scheduler;
import com.jfinal.plugin.activerecord.Db;

import java.util.Date;
import java.util.List;


/**
 * Created by lijian on 2017/10/22.
 */
public class SchedulerService {

    private static final Scheduler dao = Scheduler.dao;

    public java.util.List<Scheduler> findAll() {
        return dao.find("select * from scheduler where adminOpt<>? order by planTime asc", AdminOpt.CANCEL);
    }

    public Scheduler findById(Integer id) {
        return dao.findById(id);
    }

    public boolean save(Scheduler scheduler) {
        return scheduler.save();
    }

    public boolean update(Scheduler scheduler) {
        scheduler.setUpdateTime(new Date());
        return scheduler.update();
    }

    public int cancel(int id, String admin) {
        return Db.update("update scheduler set admin=?, adminOpt=? where id=?", admin, AdminOpt.CANCEL, id);
    }

    public boolean deleteById(Integer id) {
        return dao.deleteById(id);
    }

    public List<Scheduler> findActive() {
        // 排除已离场的、管理员取消的
        return dao.find("select * from scheduler where status<>? and adminOpt<>? order by planTime asc", Status.OUT, AdminOpt.CANCEL);
    }

}
